package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.me.NoticeAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.NotificationUtils
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.database.entity.NoticeMsg
import com.gin.kotlin.no_gay_alliance.databinding.ActivityNoticeBinding
import com.gin.kotlin.no_gay_alliance.enums.NotificationType
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.NoticeProvider
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import retrofit2.Call
import retrofit2.Response

class NoticeActivity : AbstractBindingActivity<ActivityNoticeBinding>(), RCallback<List<NoticeMsg>, Nothing> {

    private lateinit var adapter: NoticeAdapter

    override fun onCreate() {
        val b = this.b ?: return
        this.adapter = NoticeAdapter(this, handler!!)
        b.recyclerView.adapter = this.adapter
        b.swipeRefreshLayout.setOnRefreshListener { request() }
        request()
    }

    /**
     * 发送请求
     */
    private fun request() {
        val b = this.b ?: return
        b.tvWarning.visibility = View.GONE
        b.swipeRefreshLayout.isRefreshing = true
        NoticeProvider.getInstance().enqueue(callback = this)
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate(R.menu.notice, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val dao = App.INSTANCE.database.noticeMsgDao()
        val userId = App.INSTANCE.account.userId
        when (item.itemId) {
            // 清空消息
            R.id.mi_clear -> {
                // 弹窗确认是否清空提醒
                AlertDialogUtils.builder(this, getString(R.string.dialog_title_clear_notice), getString(R.string.dialog_message_clear_notice), listener = { _, which ->
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        App.INSTANCE.ngaApi.noticeApi.clear().enqueue(object : NgaCallback<List<String>> {
                            override fun onSuccess(
                                call: Call<NgaResponse<List<String>>>,
                                response: Response<NgaResponse<List<String>>>,
                                body: NgaResponse<List<String>>?,
                                data: List<String>?
                            ) {
                                data?.get(0)?.also { toast(it) }
                                dao.clear(userId)
                                adapter.clear()
                                ui {
                                    b?.tvWarning?.text = getString(R.string.message_notice_empty)
                                    b?.tvWarning?.visibility = View.VISIBLE
                                }
                                // 清空推送
                                NotificationUtils.cancel(this@NoticeActivity, NotificationType.NOTICE.ordinal)
                            }

                            override fun onError(
                                call: Call<NgaResponse<List<String>>>,
                                response: Response<NgaResponse<List<String>>>,
                                body: NgaResponse<List<String>>?,
                                errorMessage: String
                            ) {
                                toastLong(errorMessage)
                            }
                        })
                    }
                }).show()
            }
            // 全部已读
            R.id.mi_read_all -> {
                dao.read(userId)
                request()
                // 清空推送
                NotificationUtils.cancel(this@NoticeActivity, NotificationType.NOTICE.ordinal)
            }
        }

        return super.onOptionsItemSelected(item)
    }

    override fun handleIntent(intent: Intent) {
    }

    override fun onResume() {
        super.onResume()
        setToolbarTitle("提醒消息")
    }

    companion object {
        fun startActivity(context: Context) = context.startActivity(Intent(context, NoticeActivity::class.java))
    }

    override fun onSuccess(data: List<NoticeMsg>, ignoreCache: Boolean) {
        val b = this.b ?: return

        b.tvWarning.visibility = View.GONE
        b.swipeRefreshLayout.isRefreshing = false

        this.adapter.replaceData(data)
    }

    override fun onError(errorMessage: String) {
        val b = this.b ?: return
        ui {
            b.tvWarning.text = errorMessage
            b.tvWarning.visibility = View.VISIBLE
            b.swipeRefreshLayout.isRefreshing = false
        }
    }

}